package com.binance.mbxgateway.web.aop;

import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Strings.emptyToNull;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import io.swagger.annotations.ApiModel;

@Component
@Aspect
public class SwaggerApiModelTypeNameProviderAspect {

    @Around("execution(* springfox.documentation.swagger.schema.ApiModelTypeNameProvider.nameFor(..))")
    public Object doAround(ProceedingJoinPoint point) throws Throwable {
        Class<?> type = (Class<?>) point.getArgs()[0];
        ApiModel annotation = AnnotationUtils.findAnnotation(type, ApiModel.class);
        String defaultTypeName = type.getName();
        if (annotation != null) {
            return fromNullable(emptyToNull(annotation.value())).or(defaultTypeName);
        }
        return defaultTypeName;
    }
}
